package com.thug.tools;

import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.List;

import org.jbehave.core.Embeddable;
import org.jbehave.core.embedder.Embedder;
import org.jbehave.core.failures.FailingUponPendingStep;
import org.jbehave.core.io.StoryPathResolver;
import org.jbehave.core.io.UnderscoredCamelCaseResolver;
import org.jbehave.core.steps.CandidateSteps;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.junit.Test;
import org.unitils.UnitilsJUnit4;

public abstract class AbstractJBehaveTestCase extends UnitilsJUnit4 implements Embeddable {

	private List<CandidateSteps> candidateSteps = new ArrayList<CandidateSteps>();
	private Embedder embedder = new Embedder();

	protected void addSteps(Object... candidateSteps) {
		this.candidateSteps.addAll(new InstanceStepsFactory(embedder.configuration(), candidateSteps)
				.createCandidateSteps());
	}

	private List<String> storyPaths() {

		StoryPathResolver resolver = new UnderscoredCamelCaseResolver(".story").removeFromClassName("TestCase");
		String storyPath = resolver.resolve(this.getClass());

		return asList(storyPath);
	}

	public void useEmbedder(Embedder embedder) {
		// just to implement Embeddable, required to use UnderscoredCamelCaseResolver
	}

	@Test
	public void run() throws Throwable {

		embedder.configuration().usePendingStepStrategy(new FailingUponPendingStep());

		embedder.embedderControls().doGenerateViewAfterStories(true).doIgnoreFailureInStories(true)
				.doIgnoreFailureInView(true);

		embedder.useCandidateSteps(candidateSteps);
		embedder.runStoriesAsPaths(storyPaths());
	}
}
